package com.neasho.neashoadminv1.web.console.${packagePrefix};

import com.neasho.neashoadminv1.infrastructure.domain.R
import com.neasho.neashoadminv1.infrastructure.dto.${packagePrefix}.*
import com.neasho.neashoadminv1.infrastructure.permission.ApiAuth
import com.neasho.neashoadminv1.web.service.${packagePrefix}.${upperName}Service
import jakarta.validation.Valid
import org.babyfish.jimmer.spring.model.SortUtils
import org.slf4j.LoggerFactory
import org.springframework.data.domain.Page
import org.springframework.data.domain.PageRequest
import org.springframework.web.bind.annotation.*

/**
 * ${tableComment}维护
 */
@RestController
@RequestMapping("/console/${urlPrefix}")
class ${upperName}Controller(
    val ${varName}Service: ${upperName}Service
) {
    private val log = LoggerFactory.getLogger(${upperName}Controller::class.java)

    /**
     * 新
     */
    @PostMapping("/save")
    @ApiAuth([ "${permissionPrefix}:save" ])
    fun save(@Valid @RequestBody input: ${upperName}AddInput): R<Int> {
        return R.affect(${varName}Service.save(input).totalAffectedRowCount)
    }

    /**
     * 修
     */
    @PutMapping("/update")
    @ApiAuth([ "${permissionPrefix}:update" ])
    fun update(@Valid @RequestBody input: ${upperName}AddInput): R<Int> {
        return R.affect(${varName}Service.update(input).totalAffectedRowCount)
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ApiAuth([ "${permissionPrefix}:page" ])
    fun page(@Valid spec: ${upperName}PageSpec): R<Page<${upperName}PageView>> {
        return R.ok(
            ${varName}Service.page(
                spec, ${upperName}PageView::class, PageRequest.of(
                    spec.pageNumber - 1, spec.pageSize, SortUtils.toSort("id desc")
                )
            )
        )
    }

    /**
     * 查询列表
     */
    @GetMapping("/list")
    @ApiAuth([ "${permissionPrefix}:list" ])
    fun list(@Valid spec: ${upperName}ListSpec): R<List<${upperName}ListView>> {
        return R.ok(${varName}Service.list(spec, ${upperName}ListView::class))
    }

    /**
     * 查询树结构
     */
    @GetMapping("/tree")
    @ApiAuth([ "${permissionPrefix}:tree" ])
    fun tree(@Valid spec: ${upperName}ListSpec): R<List<${upperName}TreeView>> {
        return R.ok(${varName}Service.tree(spec, ${upperName}TreeView::class))
    }

    /**
     * 查询详情
     */
    @GetMapping("/detail")
    @ApiAuth([ "${permissionPrefix}:detail" ])
    fun detail(@Valid spec: ${upperName}DetailSpec): R<${upperName}DetailView> {
        return R.ok(${varName}Service.findById(spec, ${upperName}DetailView::class))
    }

    /**
     * 逻辑删除
     */
    @DeleteMapping("/delete")
    @ApiAuth([ "${permissionPrefix}:delete" ])
    fun delete(@Valid spec: ${upperName}LogicDeleteInput): R<Int> {
        return R.affect(${varName}Service.deleteByPhysical(spec.id).totalAffectedRowCount)
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batch-delete-by-ids")
    @ApiAuth([ "${permissionPrefix}:batch-delete-by-ids" ])
    fun batchDeleteByIds(@Valid spec: ${upperName}BatchDeleteInput): R<Int> {
        return R.affect(${varName}Service.batchDeleteByIds(spec.ids).totalAffectedRowCount)
    }

    /**
     * 批量修改
     */
    @PutMapping("/batch-update")
    @ApiAuth([ "${permissionPrefix}:batch-update" ])
    fun batchUpdate(@Valid @RequestBody list: List<${upperName}UpdateInput>): R<Int> {
        return R.affect(${varName}Service.batchUpdate(list))
    }

    /**
     * 批量新增
     */
    @PostMapping("/batch-save")
    @ApiAuth([ "${permissionPrefix}:batch-save" ])
    fun batchSave(@Valid @RequestBody list: List<${upperName}AddInput>): R<Int> {
        return R.affect(${varName}Service.batchSave(list))
    }

    /**
     * 导入模板
     */
    @GetMapping("/import-template")
    @ApiAuth([ "${permissionPrefix}:import-template" ])
    fun importTemplate(): R<String> {
        TODO("not yet implemented")
    }

    /**
     * 导出数据
     */
    @GetMapping("/export-data")
    @ApiAuth([ "${permissionPrefix}:export-data" ])
    fun export(): R<String> {
        TODO("not yet implemented")
    }

    /**
     * 导入数据
     */
    @PostMapping("/import-data")
    @ApiAuth([ "${permissionPrefix}:import-data" ])
    fun importData(): R<String> {
        TODO("not yet implemented")
    }

}
